package com.yingside.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class TestJListFrame2 extends JFrame {
    private JList jList1,jList2;
    private JScrollPane scrollPane1,scrollPane2;
    private JButton btnRight,btnLeft;
    private JPanel panel;
    public TestJListFrame2() {
        this.setTitle("JList赋值窗口");
        this.setBounds(300, 300, 400, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Student s1 = new Student(1,"小刘","213123211");
        Student s2 = new Student(2,"小张","213123212");
        Student s3 = new Student(3,"小关","213123213");
        Student s4 = new Student(4,"小曹","213123214");


        jList1 = new JList();
        DefaultListModel model1 = new DefaultListModel();
        model1.addElement(s1);
        model1.addElement(s2);
        model1.addElement(s3);
        model1.addElement(s4);

        jList1.setModel(model1);
        //设置只能单选
        //jList1.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);

        jList1.setFixedCellWidth(100);
        jList1.setFixedCellHeight(40);

        scrollPane1 = new JScrollPane(jList1);

        panel = new JPanel();
        panel.setLayout(new GridLayout(2,1,5,5));
        btnRight = new JButton(">>");
        btnLeft = new JButton("<<");
        panel.add(btnRight);
        panel.add(btnLeft);

        jList2 = new JList();
        jList2.setFixedCellWidth(100);
        jList2.setFixedCellHeight(40);
        DefaultListModel model2 = new DefaultListModel();
        jList2.setModel(model2);

        scrollPane2 = new JScrollPane(jList2);


        btnRight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                /*Student selectedValue = (Student) jList1.getSelectedValue();
                model2.addElement(selectedValue);
                model1.removeElement(selectedValue);*/

                List<Student> selectedValuesList = jList1.getSelectedValuesList();
                for (Student s : selectedValuesList) {
                    model2.addElement(s);
                }
            }
        });


        this.setLayout(new FlowLayout());

        this.add(scrollPane1);
        this.add(panel);
        this.add(scrollPane2);

    }

    public static void main(String[] args) {
        TestJListFrame2 jListFrame = new TestJListFrame2();
        jListFrame.setVisible(true);
    }
}
